package com.sx.pipeline.ui.mine

import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.sx.pipeline.BR
import com.sx.pipeline.R
import com.sx.pipeline.app.base.CYDataBindingBaseActivity
import com.sx.pipeline.app.model.CYBaseViewModel
import com.sx.pipeline.arouth.RoutePath
import com.sx.pipeline.databinding.ActIdentityManageBinding
import com.sx.pipeline.repos.http.res.IdentityVO
import com.sx.pipeline.repos.http.res.UnitTypeVO

/**
 * @Author: YY
 * @Date: 2022/6/27
 * @Desc: 身份管理
 */
@Route(path = RoutePath.Main.IDENTITY_MANAGE)
class IdentityManageActivity: CYDataBindingBaseActivity<ActIdentityManageBinding, IdentityManageVM>
    (R.layout.act_identity_manage), View.OnClickListener {
    
    private lateinit var identityAdapter: IdentityListAdapter

    override fun onClick(v: View) {
        when(v.id) {
            R.id.txtCancel -> {
                finish()
            }
            R.id.txtAgree -> {
                mViewModel.applyIdentity()
            }
        }
    }

    override fun initData() {
        mBinding.setVariable(BR.onClick, this)
        mViewModel.getUnitIdentity()
    }

    override fun initViewObservable() {
        super.initViewObservable()
        // 身份列表
        setIdentityList()
    }

    private fun setIdentityList() {

        observe(mViewModel.identityListEvent) {
            if (!this::identityAdapter.isInitialized) {
                mBinding.recyclerIdentity.layoutManager = LinearLayoutManager(
                    this, LinearLayoutManager.VERTICAL, false
                )
                identityAdapter = IdentityListAdapter(it as MutableList<IdentityVO>)
                mBinding.recyclerIdentity.adapter = identityAdapter
                // item点击事件
                identityAdapter.setOnItemClickListener { adapter, _, position ->
                    // 单选
                    identityAdapter.setSelectClickId(position)
                    // 选择的数据
                    val identityVO = adapter.data[position] as IdentityVO
                    mViewModel.identityField.set(identityVO.identity)
                }
            }
        }
    }


}